package com.whfc.ms.api.emp.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.PageData;
import com.whfc.common.util.RandomUtil;
import com.whfc.emp.dto.AppEmpHealthReportDTO;
import com.whfc.emp.enums.EmpHealthInfoType;
import com.whfc.emp.param.EmpHealthInfoAddParam;
import com.whfc.emp.param.EmpHealthInfoEditParam;
import com.whfc.emp.service.AppEmpHealthReportService;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.ms.api.emp.service.MsAppEmpHealthReportService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-09-02 14:22
 */
@Service
public class MsAppEmpHealthReportServiceImpl implements MsAppEmpHealthReportService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = AppEmpHealthReportService.class, version = "1.0.0")
    private AppEmpHealthReportService appEmpHealthReportService;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;


    @Override
    public PageData<AppEmpHealthReportDTO> list(Integer empId, String type, Integer pageNum, Integer pageSize) {
        return appEmpHealthReportService.list(empId, EmpHealthInfoType.parseValue(type), pageNum, pageSize);
    }

    @Override
    public void add(EmpHealthInfoAddParam param) {
        appEmpHealthReportService.add(param);
    }

    @Override
    public void edit(EmpHealthInfoEditParam param) {
        appEmpHealthReportService.edit(param);
    }

    @Override
    public void del(Integer healthInfoId) {
        appEmpHealthReportService.del(healthInfoId);
    }


    @Override
    public OssPathDTO uploadImg(MultipartFile file) {
        String filename = file.getOriginalFilename();
        String suffix = "png";
        if (StringUtils.isNotBlank(filename)) {
            suffix = filename.substring(filename.lastIndexOf(".") + 1);
        }
        String imgUrl = "";
        try {
            String path = filePathConfig.getFilePath("emp/health/report/img", RandomUtil.getRandomFileName(), suffix);
            InputStream inputStream = file.getInputStream();
            imgUrl = fileHandler.upload(path, inputStream);
        } catch (Exception e) {
            logger.warn("防疫图片上传失败", e);
        }
        return new OssPathDTO(imgUrl);
    }
}
